package com.homepig.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import static com.mongodb.client.model.Filters.*;
import static java.util.Arrays.asList;

/**
 * 查询数组
 *
 * @author fengchao
 */
public class QueryArray {
    private static MongoCollection mongoCollection;

    static {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("mongo_stu");
        mongoCollection = mongoDatabase.getCollection("inventory");

        mongoCollection.drop();

        mongoCollection.insertMany(asList(
                Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"),
                Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"),
                Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"),
                Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"),
                Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }")
        ));
    }

    /**
     * 查询匹配的数组元素,包括顺序匹配
     */
    public void queryMatchArray() {
        FindIterable findIterable = mongoCollection.find(eq("tags", asList("red", "blank")));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     * 查询匹配的数组元素，不要求顺序一致
     */
    public void queryMatchArrayIgnoreOrder() {
        FindIterable findIterable = mongoCollection.find(all("tags", asList("red", "blank")));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     *查询符合条件的元素的数组
     */
    public void queryArrayForAnElement() {
        FindIterable findIterable = mongoCollection.find(eq("tags", "red"));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     * 在数组元素上查询包含复合过滤条件的数组
     */
    public void queryArrayCompundFilterConditions() {
        FindIterable findIterable = mongoCollection.find(and(gt("dim_cm",15),lt("dim_cm",30)));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     * 只要数组中有一个元素满足所有条件，即返回该记录
     */
    public void queryArrayMeetsMultipleCriteria() {
        FindIterable findIterable = mongoCollection.find(elemMatch("dim_cm",Document.parse("{$gt:20,$lt:30}")));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     * 按数组索引位置查询元素
     */
    public void queryArrayByIndexPosition() {
        FindIterable findIterable = mongoCollection.find(gte("dim_cm.1", 30));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    /**
     * 按数组的长度进行查询
     */
    public void queryArrayByArrayLength() {
        FindIterable findIterable = mongoCollection.find(size("tags", 3));
        MongoCursor mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document document = (Document) mongoCursor.next();
            System.out.println(document.toJson());
        }
    }

    public static void main(String[] args) {
        QueryArray queryArray = new QueryArray();
        // queryArray.queryMatchArray();
        // queryArray.queryMatchArrayIgnoreOrder();
        // queryArray.queryArrayForAnElement();
        // queryArray.queryArrayByArrayLength();
        // queryArray.queryArrayByIndexPosition();
        // queryArray.queryArrayMeetsMultipleCriteria();
        queryArray.queryArrayCompundFilterConditions();
    }
}
